package com.anorak.betsy.shell;

import com.google.gson.*;
import java.io.*;
import java.util.zip.*;

public class BinaryDecompressionExample {

    private static final String BASE_PATH = "/Users/anorak/Documents/JavaProject/standalone/Akby/musicflit/src/main/java/com/anorak/betsy/shell/";

    public static void main(String[] args) throws IOException {
        // 读取经过压缩的二进制文件
        FileInputStream fileIn = new FileInputStream(BASE_PATH + "get/item.bin");

        // 创建输入流来解压缩数据
        Inflater inflater = new Inflater();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;

        while ((bytesRead = fileIn.read()) != -1) {
            inflater.setInput(new byte[]{ (byte) bytesRead });

            try {
                while (!inflater.needsInput() && !inflater.finished()) {
                    int length = inflater.inflate(buffer);

                    if (length > 0) {
                        outputStream.write(buffer, 0, length);
                    } else {
                        break;
                    }
                }
            } catch (DataFormatException e) {
                System.out.println("Error decompressing data.");
                e.printStackTrace();
            } finally {
                inflater.reset();
            }
        }

        // 关闭输入流
        fileIn.close();

        // 将解压后的字节转换为 JSON 格式并打印结果
        String jsonResult = new String(outputStream.toByteArray());
        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(jsonResult);
        System.out.println(element);
    }
}